package com.testAll;

import com.core.jdbc.SqlHelper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class testSend {
    public static void main(String[] arg) throws SQLException {

        sendTest();
    }

    /**
     * 发短信.
     *
     * @param postData 发送体
     * @param postUrl  发送url
     * @return 短信
     */
    public static String sms(String postData, String postUrl) {
        try {
            // 发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();

            // 获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "";
            }
            // 获取响应内容体
            String line = "";
            String result = "";
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            System.out.println(result);
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

    public static String sendMsg(String phone, String content) throws RuntimeException {
        String reg = "^[1]([0-9]{10})$";
        if (!phone.matches(reg)) {
            throw new RuntimeException("手机号格式不正确");
        }
        String postData = "sname=dlbaixf&spwd=123456yhn&scorpid=&sprdid=1012888&sdst=" + phone
                + "&smsg=" + content;
        String postData2 = "sname=dljinya1&spwd=JINYA6688&scorpid=&sprdid=1012808&sdst=" + phone
                + "&smsg=" + content;
        sms(postData2, "http://cf.51welink.com/submitdata/Service.asmx/g_Submit");

        return "发送成功";
    }


    public static void sendTest() throws SQLException {
        // 快到期短信提醒
        String sql = "select gid,gname,gusername,gtelphone from garden "
                + "where gid=1 ";
        List<Map<String, Object>> list = SqlHelper.queryMapList(sql,
                new HashMap<String, Object>());
        for (Map<String, Object> garden : list) {
            if (garden.get("gname") != null) {

                String p = "【金雅教育】到期通知：您园所快到期了，请及时联系金雅管理人员进行续期操作，否则到期后将无法使用本教育系统，相关负责人%s,联系电话：%s";
                String gname = garden.get("gname").toString();
                String content = String.format(p, garden.get("gusername") + "", garden.get("gtelphone") + "");
                sendMsg(garden.get("gtelphone") + "", content);

            }

        }
    }
}
